package com.eurasia.assignment.infrastructure.entity;

import java.time.LocalDateTime;

import org.hibernate.annotations.CreationTimestamp;

import com.eurasia.assignment.domain.Course;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 课程实体类
 * 数据库持久化对象
 */
@Entity
@Table(name = "course")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CourseEntity {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "course_name", nullable = false)
    private String courseName;
    
    @Column(name = "course_type", nullable = false)
    private String courseType;
    
    @Column(nullable = false)
    private Integer credit;
    
    @CreationTimestamp
    @Column(name = "create_time", nullable = false, updatable = false)
    private LocalDateTime createTime;
    
    @Column(name = "selected_count", nullable = false)
    private Integer selectedCount;
    
    @Column(nullable = true)
    private String description;

    /**
     * 从领域对象创建持久化对象
     * @param domain 领域对象
     * @return 持久化对象
     */
    public static CourseEntity fromDomain(Course domain) {
        if (domain == null) {
            return null;
        }
        CourseEntity entity = new CourseEntity();
        entity.setId(domain.getId());
        entity.setCourseName(domain.getCourseName());
        entity.setCourseType(domain.getCourseType());
        entity.setCredit(domain.getCredit());
        entity.setCreateTime(domain.getCreateTime());
        entity.setSelectedCount(domain.getSelectedCount());
        entity.setDescription(domain.getDescription());
        return entity;
    }

    /**
     * 转换为领域对象
     * @return 领域对象
     */
    public Course toDomain() {
        Course domain = new Course();
        domain.setId(this.id);
        domain.setCourseName(this.courseName);
        domain.setCourseType(this.courseType);
        domain.setCredit(this.credit);
        domain.setCreateTime(this.createTime);
        domain.setSelectedCount(this.selectedCount);
        domain.setDescription(this.description);
        return domain;
    }
} 